//xhr helper
//collects different assets for different scenes
//makes sure everyone gets what they need
//is responsible for caching them on the player's system as well


var doll = (function (_doll) {
    _doll.fetch = _doll.fetch || {};

    _doll.fetch.init = function() {
        console.log('*Fetch Initialization*');
    };

    _doll.fetch.progress = function(evt) {
        if(evt.lengthComputable) {
            var p = evt.loaded/evt.total;
            console.log(p*100 + " percent.");
        }
    };
    
    _doll.fetch.complete = function(evt) { console.log("fetch complete = ", evt); };
    _doll.fetch.failed = function(evt)   { console.log("fetch failed = ", evt); };
    _doll.fetch.canceled = function(evt) { console.log("fetch canceled by user"); };
    _doll.fetch.xhr = function(progress, complete, failed, canceled) {
        var x = new XMLHttpRequest();

        if(progress === undefined) {
            x.addEventListener("progress", _doll.fetch.progress, false);
        }
        else {
            x.addEventListener("progress", progress, false);
        }
        
        if(complete === undefined) {
            x.addEventListener("load", _doll.fetch.complete, false);
        }
        else {
            x.addEventListener("load", complete, false);
        }

        if(failed === undefined) {
            x.addEventListener("error", _doll.fetch.failed, false);
        }
        else {
            x.addEventListener("error", failed, false);
        }

        if(canceled === undefined) {
            x.addEventListener("abort", _doll.fetch.canceled, false);
        }
        else {
            x.addEventListener("abort", canceled, false);
        }
        
        return x;
    };
    //fetch a shader file either .fs or .vs format
    _doll.fetch.shader = function(filename, callback, use_load_screen) {
        var pre = "shaders/";
        var xhr;
        if(use_load_screen) {
            xhr = _doll.fetch.xhr(_doll.load_screen.progress('shader', filename),
                                  _doll.load_screen.complete('shader', filename),
                                  _doll.load_screen.failed('shader', filename),
                                  _doll.load_screen.canceled('shader', filename));
        }
        else {
            xhr = _doll.fetch.xhr();
        }

        function onreadystatechange() {
            if(xhr.readyState == 4) {
                var shader = _doll.fetch.add_shader(xhr.response);
                if(callback !== undefined) {
                    callback(shader);
                }
                delete xhr;
            }
        }

        xhr.open("get", pre + filename, true);
        xhr.send(null);
        xhr.onreadystatechange = onreadystatechange;
        onreadystatechange.call(xhr);
    };

    //fetch an audio file
    _doll.fetch.sound = function(filename, callback, use_load_screen) {
        var pre = "assets/audio/";
        var xhr;
        if(use_load_screen) {
            xhr = _doll.fetch.xhr(_doll.load_screen.progress('audio', filename),
                                  _doll.load_screen.complete('audio', filename),
                                  _doll.load_screen.failed('audio', filename),
                                  _doll.load_screen.canceled('audio', filename));
        }
        else {
            xhr = _doll.fetch.xhr();
        }

        function onreadystatechange() {
            if(xhr.readyState == 4) {
                var data = xhr.response;
                if(callback !== undefined) {
                    callback(data);
                }
                delete xhr;
            }
        }

        xhr.open("get", pre + filename, true);
        xhr.responseType = 'arraybuffer';
        xhr.send(null);
        xhr.onreadystatechange = onreadystatechange;
        onreadystatechange.call(xhr);
    };

    //adds a shader to the list returns the shader after formatting
    _doll.fetch.add_shader = function(shader) {
        shader = shader.split("\n").filter(function (e,i,a) {return e;}).join('\n');
        return shader;
    }

    return _doll;
}(doll || {}));

